'use strict';

module.exports = function(server) {
  const SysUser = server.models.SysUser;
  const SysRole = server.models.SysRole;
  const Page = server.models.Page;

  const now = new Date().getTime();
  const adminUser = {
    "username": "admin",
    "password": "Q!W@e3r4",
    "email": "admin@opsmart.com",
    "status": 1,
    "created_time": now
  };
  const adminRole = {
    "code": "000000",
    "name": "系统管理员",
    "created_date": now
  };
  const defaultModule = [
    { "code": "0101", "name": "系统用户管理", "url": "/general/user", "view_order": 1, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0102", "name": "系统角色管理", "url": "/general/role", "view_order": 2, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0103", "name": "菜单管理", "url": "/general/menu", "view_order": 3, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0104", "name": "组织机构", "url": "/general/org", "view_order": 4, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0105", "name": "人员管理", "url": "/general/person", "view_order": 5, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
  ];

  const setDefaultUserPromise = () => {
    return new Promise((resolve, reject) => {
      SysUser.findOrCreate({ where: { name: adminUser.name }}, adminUser, function(err, user, created) {
        if (created) {
          resolve(user);
          console.log('default admin user created.');
        } else if (err) {
          reject(err);
          console.log(err);
        }
      });
    });
  };
  const setDefaultRolePromise = () => {
    return new Promise((resolve, reject) => {
      SysRole.findOrCreate({ where: { code: adminRole.code }}, adminRole, function(err, role, created) {
        if (created) {
          resolve(role);
          console.log('default admin role created.');
        } else if (err) {
          reject(err);
          console.log(err);
        }
      });
    });
  };

}
